﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace library
{
    public partial class borrow_do : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        string isbn,bname;
        public borrow_do(string isbn,string bname)
        {
            InitializeComponent();
            this.isbn = isbn;
            this.bname = bname;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    
                    string bID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                    conn.Open();
                    string sql = "update book_info set borrowed_whether='是' where bID=@bID";
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Add("@bID", MySqlDbType.VarChar, 6).Value = bID;
                    int i = cmd.ExecuteNonQuery();
                    if(i>0)
                    {
                        string sql2 = "insert into borrow_info(uID,bID,borrowtime,returntime,return_whether,renew_nums) values(@uID,@bID,@borrowtime,@returntime,0,0)";
                        MySqlCommand cmd2 = new MySqlCommand(sql2, conn);
                        cmd2.Parameters.Add("@uID", MySqlDbType.Int32, 10).Value = User.uID;
                        cmd2.Parameters.Add("@bID", MySqlDbType.VarChar, 6).Value = bID;
                        DateTime currentTime = new DateTime();
                        currentTime = System.DateTime.Now;
                        DateTime returntime = new DateTime();
                        string sql3 = "select * from borrow_rule where identity=@identity";
                        MySqlCommand cmd3 = new MySqlCommand(sql3, conn);
                        cmd3.Parameters.Add("@identity", MySqlDbType.VarChar, 10).Value = User.identity;
                        MySqlDataAdapter adapter3 = new MySqlDataAdapter(cmd3);
                        DataSet dataSet3 = new DataSet();
                        adapter3.Fill(dataSet3);
                        DataTable dataTable3 = dataSet3.Tables[0];
                        int maxdays = (int)dataTable3.Rows[0]["days"];
                        returntime = currentTime.AddDays(maxdays);
                        cmd2.Parameters.Add("@borrowtime", MySqlDbType.DateTime).Value =currentTime ;
                        cmd2.Parameters.Add("@returntime", MySqlDbType.DateTime).Value =returntime;
                        int j = cmd2.ExecuteNonQuery();
                        if(j>0)
                        {
                            MessageBox.Show("借阅成功");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("借阅失败");
                        }
                        
                        
                    }
                    conn.Close();
                }
            }
        }

        private void borrow_do_Load(object sender, EventArgs e)
        {
            conn.Open();
            label1.Text = bname;
            string sql = "select * from book_info where ISBN=@isbn and borrowed_whether='否' and booked_whether='否' and on_whether='是' and borrow_whether='是' and lose_whether='否' ";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add("@isbn", MySqlDbType.VarChar, 17).Value = isbn;
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dataTable;
            this.dataGridView1.Columns["图书编号"].DataPropertyName = dataTable.Columns["bID"].ToString();
            conn.Close();
        }
    }
}
